#include <chrono>
#include <iostream>
#include <thread>

#include "log/logger.h"
#include "thread/os_thread.h"
// 示例1: 短任务函数 - 定期执行的任务
void short_task_example() {
  static int counter = 0;
  ++counter;
  LOGD("shortTaskExample task executed:%d ", counter);
  os_thread::sleep_ms(100);
}

// 示例2: 长任务函数 - 包含主循环的任务
void long_task_example(os_thread* self) {
  int counter = 0;
  while (true) {
    // 长任务需要自己调用checkAndHeart来喂狗
    AUTO_CHECK_THREAD_HEARTBEAT(self);
    ++counter;
    LOGI("Long task iteration:%d", counter);
    os_thread::sleep_ms(1000);
  }
}

void long_task_example2(os_thread* self) {
  int counter = 0;
  while (true) {
    // 长任务需要自己调用checkAndHeart来喂狗
    AUTO_CHECK_THREAD_HEARTBEAT(self);
    ++counter;
    LOGI("Long task iteration:%d", counter);
    os_thread::sleep_ms(1000);
    if (counter >= 4) {
      return;
    }
  }
}

// 示例3: 带参数的函数
void parameterized_task2(int id, const std::string& name) {
  static int counter = 0;
  ++counter;
  LOGI("run in task:%d  %s  cnt:%d", id, name.c_str(), counter);
}

// 示例3: 带参数的函数
void parameterized_task3(os_thread* self, int id, const std::string& name) {
  static int counter = 0;
  while (1) {
    AUTO_CHECK_THREAD_HEARTBEAT(self);
    ++counter;
    LOGI("run in task:%d  %s  cnt:%d", id, name.c_str(), counter);
    os_thread::sleep_ms(1000);
  }
}

void parameterized_task(int id, const std::string& name) {
  LOGI("run in task :%d  %s", id, name.c_str());
}

// 示例4: 短任务函数 - 定期执行的任务
void simple_task_example() {
  static int counter = 0;
  std::cout << "simpleTaskExample task executed: " << ++counter << std::endl;
  os_thread::sleep_ms(1 * 1000);
}

int main() {
  // 启动看门狗检测
  os_thread::start_watchdog();
  os_thread::register_watchdog_timeout_fun([](os_thread* thread) {
    std::cout << "App Watchdog timeout for thread: "
              << thread->get_thread_name() << std::endl;
  });
  // 示例1: 创建执行短任务的线程,无参数,执行后自行退出
  LOGPI("Demo1********************创建执行短任务的线程,无参数,执行后自行退出*****""********************\n");
  os_thread short_task_demo1;
  LOGI("ShortTaskDemo1 finished:%d", short_task_demo1.is_finished());
  short_task_demo1.set_thread_name("ShortTaskDemo1");
  short_task_demo1.create(short_task_example, os_thread::FUNCTION_SHORT_TASK,
                          0);
  short_task_demo1.start();
  short_task_demo1.join();
  LOGI("ShortTaskDemo1 finished:%d", short_task_demo1.is_finished());
  LOGPI("**********************************************************************************\n\n\n");

  // 示例2: 创建执行短任务的线程,无参数,1s周期执行4秒,然后主线程主动中断线程
  LOGPI("Demo2********************创建执行短任务的线程,无参数,1s周期,执行4秒,主线程中断*************************\n");
  os_thread short_task_demo2;
  LOGI("ShortTaskDemo2 finished:%d", short_task_demo2.is_finished());
  short_task_demo2.set_thread_name("ShortTaskDemo2");
  short_task_demo2.create(short_task_example, os_thread::FUNCTION_SHORT_TASK,
                          1000);
  short_task_demo2.start();
  os_thread::sleep_ms(4 * 1000);
  LOGD("call temrminate");
  short_task_demo2.terminate();
  short_task_demo2.join();
  LOGD("call join end");
  LOGI("ShortTaskDemo2 finished:%d", short_task_demo2.is_finished());
  LOGPI("**********************************************************************************\n\n\n");

  // 示例3:
  // 创建执行长任务的线程，第一个参数为os_thread*，因为要在用户线函数进行喂狗，执行4秒后，主线程中断线程
  LOGPI("Demo3**创建执行长任务的线程,第一个参数为os_thread*，因为要在用户线函数进行喂狗,执行4秒后,主线程中断线程****\n");
  os_thread long_task_demo3;
  long_task_demo3.set_thread_name("LongTaskDemo3");
  long_task_demo3.create(long_task_example, os_thread::FUNCTION_LONG_TASK, 0,
                         &long_task_demo3);
  long_task_demo3.start();
  os_thread::sleep_ms(4 * 1000);
  LOGD("call temrminate");
  long_task_demo3.terminate();
  long_task_demo3.join();
  LOGD("call join end");
  LOGPI("**********************************************************************************\n\n\n");

  // 示例4:
  // 创建执行长任务的线程，第一个参数为os_thread*，因为要在用户线函数进行喂狗，执行4秒后，主线程中断线程
  LOGPI("Demo4**创建执行长任务的线程,第一个参数为os_thread*，因为要在用户线函数进行喂狗,执行4秒后,自行退出****\n");
  os_thread long_task_demo4;
  long_task_demo4.set_thread_name("LongTaskDemo4");
  long_task_demo4.create(long_task_example2, os_thread::FUNCTION_LONG_TASK, 0,
                         &long_task_demo4);
  long_task_demo4.start();
  LOGD("call temrminate");
  long_task_demo4.join();
  LOGD("call join end");
  LOGPI("***************************************************************\n\n\n");

  // 示例5: 创建执行短任务的线程,无参数,执行后自行退出
  LOGPI("Demo5********************创建执行短任务的线程,带参数,执行后自行退出*************************\n");
  os_thread short_task_demo5;
  LOGI("ShortTaskDemo5 finished:%d", short_task_demo5.is_finished());
  short_task_demo5.set_thread_name("ShortTaskDemo5");
  short_task_demo5.create(parameterized_task, os_thread::FUNCTION_SHORT_TASK, 0,
                          42, std::string("test string parameter"));
  short_task_demo5.start();
  short_task_demo5.join();
  LOGI("ShortTaskDemo5 finished:%d", short_task_demo1.is_finished());
  LOGPI("**********************************************************************************\n\n\n");

  // 示例6: 创建执行短任务的线程,带参数,1s周期执行4秒,然后主线程主动中断线程
  LOGPI("Demo6********************创建执行短任务的线程,带参数,1s周期,执行4秒,主线程中断*************************\n");
  os_thread short_task_demo6;
  LOGI("ShortTaskDemo6 finished:%d", short_task_demo6.is_finished());
  short_task_demo6.set_thread_name("ShortTaskDemo6");
  short_task_demo6.create(parameterized_task2, os_thread::FUNCTION_SHORT_TASK,
                          1000, 22, "test string parameter");
  short_task_demo6.start();
  os_thread::sleep_ms(4 * 1000);
  LOGD("call temrminate");
  short_task_demo6.terminate();
  short_task_demo6.join();
  LOGD("call join end");
  LOGI("ShortTaskDemo6 finished:%d", short_task_demo6.is_finished());
  LOGPI("**********************************************************************************\n\n\n");

  // 示例7:
  // 创建执行短任务的线程,带参数,1s周期,执行2秒,挂起,等2秒,再恢复,等2秒,中断
  LOGPI("Demo7********************创建执行短任务的线程,带参数,1s周期,执行4秒,挂起,等2秒,再恢复,等2秒,中断*************************\n");
  os_thread short_task_demo7;
  LOGI("ShortTaskDemo7 finished:%d", short_task_demo7.is_finished());
  short_task_demo7.set_thread_name("ShortTaskDemo7");
  short_task_demo7.create(parameterized_task2, os_thread::FUNCTION_SHORT_TASK,
                          1000, 22, "test string parameter7");
  short_task_demo7.start();
  os_thread::sleep_ms(2 * 1000);
  LOGD("call pause");
  short_task_demo7.pause();
  os_thread::sleep_ms(6 * 1000);
  LOGD("call resume");
  short_task_demo7.resume();
  os_thread::sleep_ms(2 * 1000);
  LOGD("call temrminate");
  short_task_demo7.terminate();
  short_task_demo7.join();
  LOGD("call join end");
  LOGI("ShortTaskDemo7 finished:%d", short_task_demo7.is_finished());
  LOGPI("**********************************************************************************\n\n\n");

  // 示例8: 创建执行长任务的线程,带参数,
  LOGPI("Demo8***创建执行长任务的线程,带参数,0s周期,执行2秒,挂起,等4秒,再恢复,等2秒,中断*************************\n");
  os_thread short_task_demo8;
  LOGI("ShortTaskDemo8 finished:%d", short_task_demo8.is_finished());
  short_task_demo8.set_thread_name("ShortTaskDemo8");
  short_task_demo8.create(parameterized_task3, os_thread::FUNCTION_SHORT_TASK,
                          0, &short_task_demo8, 22,
                          "test string ShortTaskDemo8");
  short_task_demo8.start();
  os_thread::sleep_ms(2 * 1000);
  LOGD("call pause");
  short_task_demo8.pause();
  os_thread::sleep_ms(6 * 1000);
  LOGD("call resume");
  short_task_demo8.resume();
  os_thread::sleep_ms(2 * 1000);
  LOGD("call temrminate");
  short_task_demo8.terminate();
  short_task_demo8.join();
  LOGD("call join end");
  LOGI("ShortTaskDemo8 finished:%d", short_task_demo8.is_finished());
  LOGPI("*********************************************************************\n\n\n");
  LOGI("All threads test finished.");

  return 0;
}